<?php
require_once("ordersInit.php");

require_once ROOT_PATH.'/wappay/service/AlipayTradeService.php';
require_once ROOT_PATH.'/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php';
require_once ROOT_PATH.'/config.php';

//支付选项卡同源切换
$pay_stat = isset($_COOKIE['pay']) ? $_COOKIE['pay'] : '';

$code  = isset($_REQUEST['token']) ? trim($_REQUEST['token']) : '';
$start = isset($_REQUEST['start']) ? trim($_REQUEST['start']) : '';

if(empty($code)){
    logs('非法访问 :'.json_encode($_REQUEST));
    die('非法访问!');
}

//ip过滤
$ip   =  getIp();
$rel  =  strpos($ip,',');
if($rel){
    $ipArr = explode(',',$ip);  
    $ip    = $ipArr[0];
}

//提取订单
$url  = HOST_URL.'/mobile/api/v1/Alipay.php?mode=payOrderInfo';
$data = array('code' => urlencode($code),'ip' => $ip);
$result  = curlPost($url, $data);
if ($result['code'] != 200) {
    logs('服务异常 :'.json_encode($result));
    echo "<script> alert('".$result['msg']."');location.href='/'; </script>";
    die();
}

$info = $result['data'];
$time = $info['time'];//date("i:s",time()-strtotime($info['time']));
//检测浏览器
$stat = isWx();
if(!$stat && $pay_stat){
    $stat = true;
}

//提交支付
if($start){
   $stat = false; 
}


//$stat = true;
if(!$stat){

    //登录状态写入
    login($info['login']);

    //统一下单
    $order_id       = $info['order_id'];
    $out_trade_no   = $info['order_sn'];
    $total_amount   = $info['order_amount'];
    $subject        = '小马生鲜';
    $body           = '订单号：' . $out_trade_no;

    //测试模式
    if(AliPay_Debug){
       $total_amount = 0.01;
    }

    //超时时间 
    $timeout_express="15m";

    $payRequestBuilder = new AlipayTradeWapPayContentBuilder();
    $payRequestBuilder->setBody($body);
    $payRequestBuilder->setSubject($subject);
    $payRequestBuilder->setOutTradeNo($out_trade_no);
    $payRequestBuilder->setTotalAmount($total_amount);
    $payRequestBuilder->setTimeExpress($timeout_express);

    $payResponse = new AlipayTradeService($config);
    $result=$payResponse->wapPay($payRequestBuilder,$config['return_url'],$config['notify_url']);

}

?>

<?php if($stat){?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
<title></title>
<script src="/src/js/lib/rem.js"></script>
<style>
            *{margin:0;padding:0;}
            /*.pay_process_con{width:100%;height:1.19rem;}*/
            .pay_process{
                height:2.99rem;
                width:5.26rem;
                position:absolute;
                top:20%;
                left:8%;
                background:#ffffff;
                padding-top:0.54rem;
                border-radius:0.1rem;
                z-index: 1;
            }
            /*支付宝中间层*/
            .wxSuccess_pay{
                height:0.9rem;
                width:6.1rem;
                font-size:0.24rem;
                background:#29adfc;
                display:block;
                text-align:center;
                margin:0 0.17rem;
                border-radius:0.05rem;
                color:#ffffff;
                line-height:0.9rem;
            }
            .wxSuccess_p_con p:nth-of-type(5){
                width:100%;
                height:0.51rem;
                line-height:0.51rem;
                text-indent: 2.18rem;
                font-size:0.26rem;
                color:#202020;
                /*font-weight:700;*/
            }
            .wxSuccess_p_con p:nth-of-type(4){
                width:100%;
                height:0.51rem;
                line-height:0.51rem;
                text-indent: 1.99rem;
                font-size:0.26rem;
                color:#202020;
            /*  font-weight:700;*/
            }
            .wxSuccess_p_con p:nth-of-type(3){
                width:100%;
                height:0.51rem;
                line-height:0.51rem;
               text-align:center;
                font-size:0.26rem;
                color:#202020;
                /*font-weight:700;*/
            }
            .wxSuccess_p_con p:nth-of-type(2){
                width:100%;
                height:0;
                line-height:0;
                text-align:center;
                font-size:0.26rem;
                color:#137df6;
                font-weight: 700;
                margin-bottom:0.23rem;
            }
            .wxSuccess_p_con p:nth-of-type(1){
                width:100%;
                height:0.47rem;
                line-height:0.47rem;
                text-align:center;
                font-size:0.26rem;
                color:#29adfc;
                font-weight:normal;
            }
            .wxSuccess_p_con{
               width:100%;
               height:2.7rem;
               border-bottom:0.1rem solid #f5f4f2;
            }
            /*订单编号*/
            .wxSuccess_top span{
                width:1.48rem;
                height:0.78rem;
                color:#ffffff;
                font-size:0.23rem;
                position:absolute;
                text-align:center;
                line-height:0.78rem;
                right:0;
                top:0;
            }
            .wxSuccess_top h2{
                width:100%;
                height:0.78rem;
                line-height:0.78rem;
                text-align:center;
                color:#ffffff;
                font-size:0.27rem;
                font-weight: normal;
            }
            .wxSuccess_top img{
                display:block;
                height:0.37rem;
                width:0.41rem;
                position:absolute;
                top:0.25rem;
                left:0.25rem;
            }
            .wxSuccess_top{
                width:100%;
                height:0.78rem;
                background:#29adfc;
                position:relative;
                margin-bottom:0.33rem;
            }
            /*.pay_process_con img{
                display:block;
                height:1.19rem;
                width:1.64rem;
                position:absolute;
                top:0;
                right:0;
            }*/
            .pay_process_p{
                width:5.36rem;
                height:0.92rem;
                margin-bottom:0.3rem;
            }
            .pay_process_p p:nth-of-type(1){
                width:5.36rem;
                height:0.45rem;
                text-align:center;
                line-height:0.45rem;
                font-size:0.22rem;
                color:#000000;
            }
            .pay_process_p p:nth-of-type(2){
                width:5.36rem;
                height:0.47rem;
                text-align:center;
                line-height:0.47rem;
                font-size:0.22rem;
                color:#000000;
            }
            .pay_process_1{
                width:auto;
                height:1.14rem;
                margin:0 auto;
                border-top:1px solid #f0f0f0;
                border-bottom:1px solid #f0f0f0;
                padding-top:0.3rem;
            }
            .pay_process_1 time{
                color:#ff9595;
                font-weight:700;
            }
            .pay_process_p time{
                color:#ff9595;
                font-weight:700;
            }
            .pay_process_1 p{
                width:5.36rem;
                height:0.4rem;
                line-height:0.4rem;
                text-align:center;
                font-size:0.23rem;
            }
            /*微信支付宝的样式*/
            .member_wx1 img{
                display:block;
                width:0.57rem;
                height:0.38rem;
                position:absolute;
                top:0.2rem;
                left:0;
            }
            .member_wx1 span{
                display:block;
                height:0.26rem;
                width:0.26rem;
                border-radius:50%;
                border:0.01rem solid #d2d1cf;
                position:absolute;
                right:0;
                top:0.24rem;
            }
            .member_wx1{
                width:6.06rem;
                height:0.8rem;

                text-indent: 0.64rem;
                line-height:0.8rem;
                color:#3f3f3f;
                font-size:0.26rem;
                position:relative;
                margin:0 auto;
            }
            #zi_fu{
                width:0.52rem;
                height:0.41rem;
            }
            .bei_jing{
                height:100%;
                width:100%;
                background:#000000;
                opacity:0.5;
                position:absolute;
                top:0;
                left:0;
            }
            /*提示弹窗样式*/
            .model_w p:nth-of-type(2) span{
                flex:1;
                color:#0870d7;
                font-size:0.28rem;
                height:0.7rem;
                line-height:0.7rem;
                text-align:center;
            }
            .model_w p:nth-of-type(2) span:nth-of-type(1){
                border-right:0.01rem solid #cccccc;
            }
            .model_w p:nth-of-type(2){
                height:0.7rem;
                width:4.62rem;
                border-top:0.01rem solid #d9d4cc;
                display:flex;
            }
            .model_w p:nth-of-type(1){
                height:0.3rem;
                line-height:0.3rem;
                text-align:center;
                color:#000000;
                font-size:0.2rem;
                margin-bottom: 0.25rem
            }
            .model_w h3{
                width:4.62rem;
                height:0.56rem;
                text-align:center;
                line-height:0.56rem;
                font-size:0.24rem;
                color:#000000;
                font-weight:normal;
            }
            .model_w{
                height:1.82rem;
                width:4.62rem;
                background:#ffffff;
                border-radius:0.1rem;
                position:fixed;
                top: 38%;
                left: 14%;
                z-index:2;
            }
             .pay_mode.show{
               background:url(/src/image/blue_2.jpg) no-repeat;
               background-size: contain;
               border:0;
            }
</style>
</head>
<body>

    <div>
        <div class="wxSuccess_top">
            <a href="/"><img src="/src/image/house_m.jpg" alt=""></a>
            <h2>已提交订单</h2>
            <span id="OrderInfo">订单详情</span>
        </div>
        <div class="wxSuccess_p_con">
            <p>支付时间还剩:<time id="countDown1"></time></p>
            <p></p>

            <p>订单编号:<time><?php echo $info['order_sn'];?></time></p>
            <p>订单金额:<time>￥<?php echo $info['order_amount'];?></time></p>
            <?php if(AliPay_Debug == true) { echo '开启微信支付调试模式'; }?>

        </div>
        <div class="member_wx1">
            微信支付
            <img src="/src/image/wx.jpg" alt="">
            <span class="pay_mode" data-id="1"></span>
        </div>
        <p style="height:0.02rem;background:#f0f0f0"></p>
        <div class="member_wx1">
            支付宝支付
            <img src="/src/image/zifu.jpg" alt="" id="zi_fu">
            <span class="pay_mode show"  data-id="2"></span>
        </div>
        <p style='margin-bottom:0.6rem;height:0.1rem;background:#f5f4f2'></p>
        <span class="wxSuccess_pay" id="payBut" data-id="2">立即支付</span>

        <div class="pay_process" style="display:none;">
            <!--<div class="pay_process_con" style="display:none;">
                <img src="src/image/arrow_2.jpg" alt="">
            </div>-->
            <div class="pay_process_p">
                <p>请在右上角箭头指向的<time>菜单</time>菜单中选择"<time>浏览器"打开</time></p>
                <p>在浏览器中完成支付</p>
            </div>
            <div class="pay_process_1">
                     <p>是否已经完成付款?</p>
                     <p>请确保支付完成后,点击<time id="pay_success">完成付款</time></p>
            </div>
        </div>
    </div>
    <!--背景弹窗-->
    <div class="bei_jing" style="display:none"></div>
    <!--提示弹窗-->
    <div class="model_w" style="display:none">
        <h3>提示</h3>
        <p>你的支付是否已经完成</p>
        <p>
            <span>支付遇到问题</span>
            <span  onclick="to_order()">支付已经完成</span>
        </p>
    </div>

    </body>



<script src="/src/js/lib/jquery-3.0.0.min.js"></script>
<script type="text/javascript"> 
//根域名
const  HostUrl  = window.location.protocol+'//'+window.location.host;
var    order_id = "<?php echo $info['order_id'];?>";
var    code     = "<?php echo urlencode($code); ?>";
var    date     = "<?php echo $time;?>"
//判断是否微信浏览器
function isWeiXin(){
  var ua = window.navigator.userAgent.toLowerCase();
  if (ua.match(/MicroMessenger/i) == 'micromessenger') {
      return true;
  } else {
      return false;
  }
}
//支付倒计时时间显示计算
 function getCountDown(timestamp){
             setInterval(function(){
                 var nowTime = new Date();
                 var endTime = timestamp.getTime() + (1000*30*60);

                 var t = endTime - nowTime.getTime();
     	           var min=Math.floor(t/1000/60%60);
                    var sec=Math.floor(t/1000%60);

                 if (min < 10) {
                      min = "0" + min;
                 }
                 if (sec < 10) {
                      sec = "0" + sec;
                 }
                 var countDownTime =min + ":" + sec;
                 $("#countDown1").html(countDownTime);

             },1000);
         }

//调用函数：
getCountDown(new Date(date.replace(/-/g,"/")));//调取时间倒计时的函数


//支付显示
function view_But(){
     $('.bei_jing').show();
     $('.model_w').show();
}   

//支付完成
function to_order(){
    check_pay();
}

//支付疑问
function question(){
    $('.bei_jing').hide();
    $('.model_w').hide();
}

//完成付款验证
function check_pay(){
  let url  =  HostUrl+'/php/v1/orderApi.php?mode=check_pay'; 
  let Data =  {"order_id":order_id};
  $.ajax({
       type: "POST",
       url: url,
       async: true,
       data: Data,
       success: function(temp){
            if(temp.code == 200){
                window.location.href= "/pay_success?order_id="+order_id;      
            }  
       }
    }); 
}

$(function(){

    //切换支付方式
    $('.pay_mode').click(function(){

        //选中
        $('.pay_mode').removeClass('show');
        $(this).addClass('show');

        //附加
        let pay_id = $(this).attr('data-id');

        //注入
        $("#payBut").attr('data-id',pay_id);
        
        //微信切换
        if(pay_id == 1){
           window.location.href= "/php/v1/wxPay.php?order_id="+order_id;
        }

        //支付宝切换
        if(pay_id == 2){
           window.location.href= "/alipay/aliPay.php?token="+code;
        }

    })


    //支付宝支付提示
    $("#payBut").click(function(){

        let stat = isWeiXin();
        if(stat){
            $('.bei_jing').show();
            $(".pay_process").show();

            setTimeout(function(){
               $('.bei_jing').click();
               view_But();
            },8000);

        }else{
            window.location.href= "/alipay/aliPay.php?token="+code+'&start=1';
        }
     
    })

    $('.bei_jing').click(function(){
        $(".pay_process").hide();
        $(this).hide();
        $('.model_w').hide();
    })

    //支付完成
    $("#pay_success").click(function(){
        check_pay();
    })

   //跳转详情
   $("#OrderInfo").click(function(){
      window.location.href='/Orderdetails?order_id='+order_id;
   })


})
</script>

</html>
<?php } ?>
